package leetcode.code;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;

public class Solution506_2 implements IDebug {

	private final String F = "Gold Medal";
	private final String S = "Silver Medal";
	private final String T = "Bronze Medal";

	public String[] findRelativeRanks(int[] score) {

		int LEN = score.length;
		String[] ans = new String[LEN];
		Map<Integer, Integer> map = new HashMap<>();
		for (int i = 0; i < LEN; i++) {
			map.put(score[i], i);
		}
		Arrays.sort(score);
		int i = LEN - 1;
		if (i >= 0) {
			int s = score[i--];
			ans[map.get(s)] = F;
		}
		if (i >= 0) {
			int s = score[i--];
			ans[map.get(s)] = S;
		}
		if (i >= 0) {
			int s = score[i--];
			ans[map.get(s)] = T;
		}

		for (int ranking = 4; i >= 0; i--, ranking++) {
			ans[map.get(score[i])] = String.valueOf(ranking);
		}
		return ans;
	}

	@Override
	public void debug1() {
		String[] r = this.findRelativeRanks(HelpDebug.str2array3("[5,4,3,2,1]"));
		String[] e = HelpDebug.str2array2("[\"Gold Medal\",\"Silver Medal\",\"Bronze Medal\",\"4\",\"5\"]");
		HelpDebug.compare(e, r);
	}

	@Override
	public void debug2() {
		String[] r = this.findRelativeRanks(HelpDebug.str2array3("[10,3,8,9,4]"));
		String[] e = HelpDebug.str2array2("[\"Gold Medal\",\"5\",\"Bronze Medal\",\"Silver Medal\",\"4\"]");
		HelpDebug.compare(e, r);
	}

	@Override
	public void debug3() {
		String[] r = this.findRelativeRanks(HelpDebug.str2array3("[1]"));
		String[] e = HelpDebug.str2array2("[\"Gold Medal\",\"5\",\"Bronze Medal\",\"Silver Medal\",\"4\"]");
		HelpDebug.compare(e, r);
	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution506_2 so = new Solution506_2();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
